import Vue from 'vue'
import Vuex from 'vuex'
import storage from '@/utils/storage'
import * as LoginApi from '@/api/login'
import getters from './getters'
Vue.use(Vuex)

// 登陆成功后执行
const loginSuccess = (commit, { token, id }) => {	
	console.log('ceshi',{ token, id })
  // 过期时间30天
  const expiryTime = 30 * 86400
  // 保存tokne和userId到缓存
  storage.set("userId", id, expiryTime)
  storage.set("token", token, expiryTime)
  // 记录到store全局变量
  commit('SET_TOKEN', token)
  commit('SET_USER_ID', id)
}

const store = new Vuex.Store({
	state: {
	  // 用户认证token
	  token: '',
	  // 用户ID
	  userId: ''
	},
	
	mutations: {
	  SET_TOKEN: (state, value) => {
	    state.token = value
		storage.set("token", value)
	  },
	  SET_USER_ID: (state, value) => {
		storage.set("userId", value)
	    state.userId = value
	  },
		SET_PLATFORM: (state, value) => {
		state.platform = value
	  },
	  SET_REFEREE_ID: (state, value) => {
		state.refereeId = value
	  },
	},
	actions: {
		updateUser({ commit }, data){
			return new Promise((resolve, reject) => {
			   LoginApi.updateUser(data)
					.then(response => {
					  const result = response.data
					  result.token = storage.get("tempToken")
					  loginSuccess(commit, result)
					  resolve(response)
					})
					.catch(reject)
				})
		},
		   // 用户登录 (普通登录: 输入手机号和验证码)
		Login({ commit }, data) {
		  return new Promise((resolve, reject) => {
			LoginApi.login(data)
			  .then(response => {
				const result = response.data
				console.log(result)
				loginSuccess(commit, result)
				resolve(response)
			  })
			  .catch(reject)
		  })
		},
		LoginMpWxMobile({ commit }, data) {
		   return new Promise((resolve, reject) => {
					   LoginApi.LoginMpWxMobile(data)
							.then(response => {
							  const result = response.data.userinfo
							  loginSuccess(commit, result)
							  resolve(response)
							})
							.catch(reject)
						})
		},
	    // 微信小程序一键授权登录 (获取用户基本信息)
	    LoginMpWx({ commit }, data) {
			loginSuccess(commit, data)
			  // LoginMpWx(data).then(response => {
			  //  const result = response.data
			  //  loginSuccess(commit, result)
			  //  resolve(response)
			  // }).catch(reject)
	   },
	   LoginWxOfficial({ commit }, data) {
		   console.log(data)
	   			loginSuccess(commit, data)
	   			  // LoginMpWx(data).then(response => {
	   			  //  const result = response.data
	   			  //  loginSuccess(commit, result)
	   			  //  resolve(response)
	   			  // }).catch(reject)
	   },
	    // 退出登录
	    Logout({ commit }, data) {
	      const store = this
		  // 删除缓存中的tokne和userId
		  storage.remove("userId")
		  storage.remove("token")
		  // 记录到store全局变量
		  commit('SET_TOKEN', '')
		  commit('SET_USER_ID', null)
	      // return new Promise((resolve, reject) => {
	      //   if (store.getters.userId > 0) {
	      //     // 删除缓存中的tokne和userId
	      //     storage.remove(USER_ID)
	      //     storage.remove(ACCESS_TOKEN)
	      //     // 记录到store全局变量
	      //     commit('SET_TOKEN', '')
	      //     commit('SET_USER_ID', null)
	      //     resolve()
	      //   }
	      // })
	    }
	  },
	getters
})

export default store